﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace SimpleCrawler.Tools
{
    public class HtmlHelper
    {
        /// <summary>
        /// 清洗标签
        /// </summary>
        /// <param name="htmlSource"></param>
        /// <returns></returns>
        public static string HtmlCleanTag(string htmlSource)
        {
            string pattern = @"(?s)<script.*?</script>";
            string pattern2 = @"(?s)<!--.*?-->";
            string pattern3 = @"(?s)<iframe.*?</iframe>";
            string pattern4 = @"(?s)(?s)<style.*?</style>";
            htmlSource = Regex.Replace(htmlSource, pattern, string.Empty, RegexOptions.IgnoreCase);
            htmlSource = Regex.Replace(htmlSource, pattern2, string.Empty, RegexOptions.IgnoreCase);
            htmlSource = Regex.Replace(htmlSource, pattern3, string.Empty, RegexOptions.IgnoreCase);
            htmlSource = Regex.Replace(htmlSource, pattern4, string.Empty, RegexOptions.IgnoreCase);
            return htmlSource;
        }
        /// <summary>
        /// 清洗标签
        /// </summary>
        /// <param name="htmlSource"></param>
        /// <returns></returns>
        public static string HtmlCleanWapTag(string htmlSource)
        {
            string pattern5 = @"(?is)(<form[^>]*>(.*?)</form>)";
            string pattern6 = @"(?is)(<object[^>]*>(.*?)</object>)";
            string pattern7 = @"(?i)(<embed[^>]*/>)";
            htmlSource = Regex.Replace(htmlSource, pattern5, string.Empty, RegexOptions.IgnoreCase);
            htmlSource = Regex.Replace(htmlSource, pattern6, string.Empty, RegexOptions.IgnoreCase);
            htmlSource = Regex.Replace(htmlSource, pattern7, string.Empty, RegexOptions.IgnoreCase);
            return htmlSource;
        }

        /// <summary>
        /// 移除特殊字符
        /// </summary>
        /// <param name="htmlSource"></param>
        /// <returns></returns>
        public static string RemoveSpecialChars(string htmlSource)
        {
            if (string.IsNullOrEmpty(htmlSource))
            {
                return string.Empty;
            }
            htmlSource = Regex.Replace(htmlSource, @"\s", "");
            htmlSource = Regex.Replace(htmlSource, @"&nbsp;", "");
            return htmlSource;

        }
    }
}
